package com.xrui.hbase.compression;

import com.moz.kiji.delegation.Lookups;
import com.moz.kiji.delegation.NamedProvider;

import java.io.IOException;

/**
 * Interface for byte array compression.
 */
public interface CompressionProvider extends NamedProvider {
    /**
     * Compress a array of bytes
     *
     * @param data The array of bytes to be compressed.
     * @return The compressed array of bytes.
     * @throws IOException when cannot be compressed.
     */
    public byte[] compress(byte[] data) throws IOException;

    /**
     * Decompress a array of bytes
     *
     * @param compressedData The array of bytes to be decompressed.
     * @return The decompressed array of bytes.
     * @throws IOException when cannot be decompressed.
     */
    public byte[] decompress(byte[] compressedData) throws IOException;

    /**
     * Provider for the default CompressionProvider factory.
     */
    public static final class Factory {
        /**
         * Utility class may not be instantiated.
         */
        private Factory() {
        }

        /**
         * Returns a CompressionProvider for the appropriate name of provider.
         *
         * @param name for the CompressionProvider instance to build with the factory.
         * @return the CompressionProvider.
         */
        public static CompressionProvider create(String name) {
            if (null == name || name.isEmpty()) {
                return new NoneCompressionProvider();
            } else {
                return Lookups.getNamed(CompressionProvider.class).lookup(name);
            }
        }
    }
}
